package com.cumt.framework.dubbo.impl;

import com.cumt.framework.domain.BaseEntity;
import com.cumt.framework.dubbo.DubboBaseInterface;
import com.cumt.framework.exception.DubboProviderException;
import com.cumt.framework.exception.ExceptionFactory;
import com.cumt.framework.mapper.BaseInterfaceMapper;
import com.cumt.framework.rest.view.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.ExecutionException;

/**
 * Created by caicf on 2016/12/27.
 */
public abstract class DubboBaseInterfaceImpl<Entity extends BaseEntity> implements DubboBaseInterface<Entity> {

    private static final Logger log = LoggerFactory.getLogger(DubboBaseInterfaceImpl.class);

    protected abstract BaseInterfaceMapper<Entity> getBaseInterfaceMapper();

    @Override
    public Page findByPage(Page page, Map<String, Object> map) throws DubboProviderException {
        Page result = page;
        try {
            int count = this.getBaseInterfaceMapper().getCount(map);
            result.setTotal(count);
            map.put("startRowNum", result.getStartRowNum());
            map.put("pageSize", result.getPageSize());
            List<Entity> entities = this.getBaseInterfaceMapper().findByPage(map);
            result.setRows(entities);
        } catch (Exception e) {
            log.error("DubboBaseInterfaceImpl findByPage ", e);
            throw new DubboProviderException(e.getMessage(), e);
        }
        return result;
    }

    @Override
    public boolean save(Entity entity) throws DubboProviderException {
        boolean flag = false;
        try {
            entity.setCreateDate(new Date());
            int res = this.getBaseInterfaceMapper().save(entity);
            if (res != 0) {
                flag = true;
            }
        } catch (Exception e) {
            log.error("DubboBaseInterfaceImpl save ", e);
            throw new DubboProviderException(e.getMessage(), e);
        }
        return flag;
    }

    @Override
    public boolean update(Entity entity) throws DubboProviderException {
        boolean flag = false;
        try {
            entity.setCreateDate(new Date());
            int res = this.getBaseInterfaceMapper().update(entity);
            if (res != 0) {
                flag = true;
            }
            if (true) {
                throw new RuntimeException();
            }
            this.getBaseInterfaceMapper().save(entity);
        } catch (Exception e) {
            log.error("DubboBaseInterfaceImpl update ", e);
            throw ExceptionFactory.wrapException(e);
        }
        return flag;
    }

    @Override
    public boolean deleteById(List<Integer> ids) throws DubboProviderException {
        boolean flag = true;
        try {
            int result = this.getBaseInterfaceMapper().deleteById(ids);
            if (result == 0) {  //返回0则表示没有删除记录
                flag = false;
            }
        } catch (Exception e) {
            flag = false;
            log.error("DubboBaseInterfaceImpl deleteById ", e);
            throw new DubboProviderException(e.getMessage(), e);
        }
        return flag;
    }

    @Override
    public Entity findById(int id) throws DubboProviderException {
        Entity entity = null;
        try {
            entity = this.getBaseInterfaceMapper().findById(id);
        } catch (Exception e) {
            log.error("DubboBaseInterfaceImpl findById ", e);
            throw new DubboProviderException(e.getMessage(), e);
        }
        return entity;
    }

    @Override
    public List<Entity> findAll() throws DubboProviderException {
        List<Entity> list = null;
        try {
            list = this.getBaseInterfaceMapper().findAll();
        } catch (Exception e) {
            log.error("DubboBaseInterfaceImpl findAll ", e);
            throw new DubboProviderException(e.getMessage(), e);
        }
        return list;
    }

    @Override
    public List<Entity> findByMap(Map<String, Object> map) throws DubboProviderException {
        List<Entity> result = null;
        try {
            result = this.getBaseInterfaceMapper().findByMap(map);
        } catch (Exception e) {
            log.error("DubboBaseInterfaceImpl findByMap ", e);
            throw new DubboProviderException(e.getMessage(), e);
        }
        return result;
    }

}
